package com.lz.ovuola.web.api;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lz.ovuola.service.SysSecurityService;
import com.lz.ovuola.util.annotation.ApiRestController;
import com.lz.ovuola.util.threadlocal.UserContextHolder;

@RestController
public class SysSecurityController extends AbstractRestController {
	@Resource
	private SysSecurityService sysSecurityService;

	/**
	 * 发送验证码
	 * 
	 * @param userCommand
	 * @return
	 */
	@ApiRestController(isRight = false)
	@RequestMapping(value = "smsCode", method = RequestMethod.GET)
	public String get(UserCommand userCommand) {
		return sysSecurityService.getSms(userCommand.getUserName(),
				userCommand.getMark());
	}

	/**
	 * 登录
	 * 
	 * @param userCommand
	 * @return
	 * @throws Exception
	 */
	@ApiRestController(isRight = false)
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String save(@RequestBody UserCommand userCommand) throws Exception {
		return sysSecurityService.login(userCommand.getUserName(),
				userCommand.getPassword());
	}

	/**
	 * 第三方登录
	 * 
	 * @param userCommand
	 * @return
	 * @throws Exception
	 */
	@ApiRestController(isRight = false)
	@RequestMapping(value = "thirdPartyLogin", method = RequestMethod.POST)
	public String login(@RequestBody UserCommand userCommand) throws Exception {
		return sysSecurityService.save(userCommand.getAppId(),
				userCommand.getNickName(), userCommand.getSource(),
				userCommand.getHeadImage(), userCommand.getDevice());
	}

	/**
	 * 验证用户密码
	 * 
	 * @param userCommand
	 * 
	 * @return
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "verifyPassword", method = RequestMethod.POST)
	public String findPassword(@RequestBody UserCommand userCommand) {

		return sysSecurityService.findPassword(userCommand.getPassword(),
				UserContextHolder.get().getUser());
	}

	/**
	 * 修改绑定手机号
	 * 
	 * @param userCommand
	 * @return
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "changePhone", method = RequestMethod.PUT)
	public String updateUserName(@RequestBody UserCommand userCommand) {
		return sysSecurityService.updateUserName(userCommand.getUserName(),
				userCommand.getCode(), userCommand.getMark(), UserContextHolder
						.get().getUser());
	}

	/**
	 * 忘记密码
	 */

	@ApiRestController(isRight = true)
	@RequestMapping(value = "forgetPassword", method = RequestMethod.PUT)
	public String updatePassword(@RequestBody UserCommand uesrCommand) {
		return sysSecurityService.updatePassword(uesrCommand.getUserName(),
				uesrCommand.getPassword(), uesrCommand.getCode(),
				uesrCommand.getMark(), UserContextHolder.get().getUserId());
	}

	/**
	 * 退出登录
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "logout", method = RequestMethod.DELETE)
	public String delete() {
		return sysSecurityService
				.logoutLogin(UserContextHolder.get().getUser());
	}

}
